package com.avic.rong.course.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.course.bean.PeiXunSortBean;
import com.avic.rong.course.showapi.PeiXunSortApi;
import com.avic.rong.fragment.course.FragmentVideoCourse;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import q.rorbin.verticaltablayout.VerticalTabLayout;

/**
 * 视频课
 */
public class PeiXunVideoActivity extends BaseActivity {

    @BindView(R.id.peixun_video_vtb)
    VerticalTabLayout verticalTabLayout;
    @BindView(R.id.peixun_video_vp)
    ViewPager peixunVideoViewPager;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_root_top)
    RelativeLayout ll_root_top;
//    private PeiXunVideoVpAdapter vpAdapter;
    private List<PeiXunSortBean.ClassifyListBean> dataList = new ArrayList<>();
    private List<FragmentVideoCourse> videoCourseFragmentList = new ArrayList<>();
    private  MyPagerAdapter myPagerAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_pei_xun_video;
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_pei_xun_video);
        ButterKnife.bind(this);
        tvTitle.setText("视频课");
        ivRight.setImageResource(R.mipmap.gouwuche_dot);
        initTitleData();
    }

    private void initTitleData() {
        PeiXunSortApi classMuLuApi = new PeiXunSortApi(this, simpleOnNextListener, String.valueOf(10));
        classMuLuApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(classMuLuApi);
    }

    //提交报名咨询数据监听
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(PeiXunVideoActivity.this, subjects.getMsg());
                Log.e("cjx", subjects.getMsg());
                return;
            }
            dataList.clear();
            PeiXunSortBean bean = (PeiXunSortBean) subjects.getResult();
            dataList.addAll(bean.getClassifyList());
//            vpAdapter = new PeiXunVideoVpAdapter(PeiXunVideoActivity.this, dataList);
//            peixunVideoViewPager.setAdapter(vpAdapter);
//            verticalTabLayout.setupWithViewPager(peixunVideoViewPager);
//            VedioTabAdapter vedioTabAdapter = new VedioTabAdapter(PeiXunVideoActivity.this, dataList);

//            List<Fragment> fragmentList = new ArrayList<>();
            for (int index = 0; index < dataList.size(); index++){
                videoCourseFragmentList.add(FragmentVideoCourse.getInstance(dataList.get(index).getName(),PeiXunVideoActivity.this,String.valueOf(dataList.get(index).getId())));
            }

//          verticalTabLayout.setupWithFragment(getSupportFragmentManager(), fragmentList, vedioTabAdapter);

            myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
            peixunVideoViewPager.setAdapter(myPagerAdapter);
            verticalTabLayout.setupWithViewPager(peixunVideoViewPager);
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "培训列表数据：" + e.getMessage());
        }
    };


    private class MyPagerAdapter extends FragmentPagerAdapter {


        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return videoCourseFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return dataList.get(position).getName();
        }

        @Override
        public Fragment getItem(int position) {
            return videoCourseFragmentList.get(position);
        }
    }




    @OnClick(R.id.iv_back)
    public void goBack(View view){
        onBackPressed();
    }


    @OnClick(R.id.iv_right)
    public void goDirect(View view){
        showPopup(PeiXunVideoActivity.this, ll_root_top);
    }




    @Override
    protected void addListener() {

    }
}
